package com.yuting.practice.letcode;

import java.util.Comparator;
import java.util.PriorityQueue;

public class 查找数据流中的中位数 {

    int count = 0;

    private PriorityQueue<Integer> minDump = new PriorityQueue<>();

    private PriorityQueue<Integer> maxDump = new PriorityQueue<>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
           return o2.compareTo(o1);
        }
    });

    public void readStream(int in){
       if(count % 2 == 0){
           minDump.offer(in);
           maxDump.offer(minDump.poll());
       }else {
           maxDump.offer(in);
           minDump.offer(maxDump.poll());
       }
       count++;
        System.out.println(getMedian());
    }

    public int getMedian(){
       return maxDump.peek();
    }


    public static void main(String[] args) {
        查找数据流中的中位数 c = new 查找数据流中的中位数();

        c.readStream(2);
        c.readStream(3);
        c.readStream(4);
        c.readStream(5);
        c.readStream(6);
        c.readStream(7);
        c.readStream(8);


    }

}
